package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import dbConnection.Database;

public class AdminUserSearchResult extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Database DB;

	/**
	 * The userID for the user represented in this search result
	 */
	public int userID;

	public AdminUserSearchResult(final Database DB, final int userID) {
		this.DB = DB;
		this.userID = userID;
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		final JTextArea summary = new JTextArea();
		summary.setText(getSummary());
		
		setMaximumSize(new Dimension(600,60));

		JButton pardonFines = new JButton("Pardon Fines");
		pardonFines.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Connection connection = DB.con;

				String zeroFines = "UPDATE user SET Fines = 0 "
						+ "WHERE user.UserId =" + userID;

				//System.out.println(setUserFinesto0_onthis_user_ID);
				PreparedStatement ps;
				try {
					ps = connection.prepareStatement(zeroFines);
					ps.executeUpdate();

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				summary.setText(getSummary());
			}
		});

		JButton PWReset = new JButton("Reset Password");
		PWReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Connection connection = DB.con;

				String query_to_change_this_users_password_to_password = "UPDATE user SET Password = 'password' "
						+ "WHERE user.UserId =" + userID;

//				System.out.println(setUserFinesto0_onthis_user_ID);
				PreparedStatement ps;
				try {
					ps = connection
							.prepareStatement(query_to_change_this_users_password_to_password);
					ps.executeUpdate();

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				summary.setText(getSummary());
			}
		});

		add(summary);
		add(pardonFines);
		add(PWReset);

	}

	public String getSummary() {

		Connection connection = DB.con;

		String query = "SELECT UserId,Name,Phone,Email,Address,Fines FROM user WHERE user.UserId ="
				+ userID + "";

		// System.out.println(query);
		String result = "";
		PreparedStatement ps;
		try {
			ps = connection.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			//System.out
					//.printf(" ID      NAME      PHONE    EMAIL      ADDRESS      FINES\n  ");
			
			while (rs.next()) {
				// Read values using column index
				int id = rs.getInt(1);
				String name = rs.getString("Name");
				String phone = rs.getString("Phone");
				String email = rs.getString("Email");
				String address = rs.getString("Address");
				float fines = rs.getFloat(6);
				// String password = rs.getString("Password");

				//System.out.printf("%d, %s, %s, %s, %s, %.0f\n", id, name,
					//	phone, email, address, fines);
				
				
				result = result+ "ID: " + id + "   NAME: " + name + "   \nPHONE: "
						+ phone + "    EMAIL: " + email + "    \nADDRESS: "
						+ address + "    FINES: " + fines;
			}

		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		return result;
	}
}
